<?php
namespace App\Http\Controllers;

use App\Service\SaleService;
use Illuminate\Http\Request;

class SaleController extends Controller
{
    //列表
    public function index()
    {
        $res = SaleService::index();
        if ($res['code'] < 0) {
            return self::toJson(-1, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //导入接口
    public function import(Request $request)
    {
        $unitId = isset($_GET['unitId'])&&$_GET['unitId'] ? $_GET['unitId'] : '';
        //$alls = $request->all();
        $body = isset($_POST['body'])&&$_POST['body'] ? $_POST['body'] : '';
        if (!$unitId) {
            return self::toJson(-1, '商铺编号');
        }
        if (empty($body)) {
            return self::toJson(-1, '没有参数体');
        }
        $bodys = explode('_', $body);
        $pas['unitId'] = $unitId;
        $pas['json'] = json_encode($bodys, JSON_UNESCAPED_UNICODE);
        $pas['saleBody'] = $bodys[0];
        $pas['saleDetail'] = isset($bodys[1]) ? json_encode($bodys[1], JSON_UNESCAPED_UNICODE) : '';
        $pas['salePay'] = isset($bodys[2]) ? json_encode($bodys[2], JSON_UNESCAPED_UNICODE) : '';
        $res = SaleService::import($pas);
        if ($res['code'] < 0) {
            return self::toJson(-1, $res['msg']);
        }
        return self::toJson();
    }
}